package com.example.service.impl;

import com.example.entity.Auth;
import com.example.entity.Role;
import com.example.entity.User;
import com.example.entity.param.JWTParam;
import com.example.mapper.AuthMapper;
import com.example.mapper.RoleMapper;
import com.example.mapper.UserMapper;
import com.example.service.LoginService;
import com.example.utils.JwtUtil;
import com.example.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class LoginServiceImpl implements LoginService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    AuthMapper authMapper;


    @Override
    public int register(User user) {
        userMapper.insert(user);
        return user.getId();
    }

    @Override
    public HashMap<String, String> getToken(String phone, String password) {
        HashMap<String, String> map = new HashMap<>();
        User user = userMapper.selectByPhone(phone);
        if (user == null) {
            map.put("info", "your phone is none");
            return map;
        }
        try {
            if (MD5Util.getMD5(password).equals(user.getPassword())) {
                JWTParam param = new JWTParam();
                param.setUserid(user.getId());
                param.setPhone(user.getPhone());
                Role role = roleMapper.selectByPrimaryKey(user.getRoleId());
                param.setRole(role.getRolename());
                List<Auth> auths = authMapper.selectByRoleId(user.getRoleId());
                List<String> list = new ArrayList<>();
                for (Auth auth : auths) {
                    String path = auth.getPath();
                    list.add(path);
                }
                param.setAuth(list);
                String sign = JwtUtil.sign(param);
                map.put("info", "success");
                map.put("token", sign);
                return map;
            }
            map.put("info", "your password is error");
            return map;
        } catch (Exception e) {
            map.put("info", "your phone is none");
            e.printStackTrace();
            return map;
        }
    }

    @Override
    public HashMap<String, String> phoneCode(String phone) {
        HashMap<String, String> map = new HashMap<>();
        User user = userMapper.selectByPhone(phone);
        if (user == null) {
            map.put("info", "your phone is none");
            return map;
        }
        try {
            JWTParam param = new JWTParam();
            param.setUserid(user.getId());
            param.setPhone(user.getPhone());
            Role role = roleMapper.selectByPrimaryKey(user.getRoleId());
            param.setRole(role.getRolename());
            List<Auth> auths = authMapper.selectByRoleId(user.getRoleId());
            List<String> list = new ArrayList<>();
            for (Auth auth : auths) {
                String path = auth.getPath();
                list.add(path);
            }
            param.setAuth(list);
            String sign = JwtUtil.sign(param);
            map.put("info", "success");
            map.put("token", sign);
            return map;
        } catch (Exception e) {
            map.put("info", "your phone is none");
            e.printStackTrace();
            return map;
        }
    }

    @Override
    public Boolean checkIsRegisterByPhone(String phone) {
        User user = userMapper.selectByPhone(phone);
        if (user == null) {
            return false;
        }
        return true;
    }
}
